﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoolERP_Entities;

namespace CoolERP_DAL
{
    public interface IXSHTMXBDAL : IDAL<XSHTMXB>
    {
        List<XSHTMXBModel> GetXSHTMXB(Func<XSHTMXBModel, bool> exp, string orderExpr, int? pageIndex, int pageSize, out int totalCount);
        List<XSHTMXBModel> GetSCJH_HTMXB(Func<XSHTMXBModel, bool> exp, string orderExpr, int? pageIndex, int pageSize, out int totalCount);
    }

    public class XSHTMXBDAL : BaseDAL<XSHTMXB>, IXSHTMXBDAL
    {
        public XSHTMXBDAL(IUnitOfWork context)
            : base(context)
        {
        }

        public List<XSHTMXBModel> GetXSHTMXB(Func<XSHTMXBModel, bool> exp, string orderExpr, int? pageIndex, int pageSize, out int totalCount)
        {
            var result = from mxb in dataContext.XSHTMXB.Include("XSHT")
                         join c in dataContext.vCP on mxb.CPID equals c.ID
                         join k in dataContext.ZL_KH on mxb.XSHT.KHID equals k.ID
                         select new XSHTMXBModel
                         {
                             ID = mxb.ID,
                             XSHTID = mxb.XSHTID,
                             HTZT = mxb.XSHT.HTZT,
                             HTBH = mxb.XSHT.BH,
                             KHJC = k.JIANCHENG,
                             DHSJ = mxb.XSHT.DHSJ,
                             CHSJ = mxb.XSHT.CHSJ,
                             CPID = mxb.CPID,
                             CPBH = c.BH,
                             CPMC = c.CPMC,
                             CPGG = c.CPGG,
                             CPYS = c.YS,
                             CPLX = c.CPLXMC,
                             SL = mxb.SL,
                             DJ = mxb.DJ
                         };
            result = result.Where(exp).AsQueryable();
            totalCount = result.Count();
            if (!string.IsNullOrEmpty(orderExpr))
                result = SortingExtensions.OrderUsingSortExpression(result, orderExpr);
            if (pageIndex == null)
                return result.ToList();
            else
                return result.Skip(Convert.ToInt32(--pageIndex) * pageSize).Take(pageSize).ToList();
        }

        public List<XSHTMXBModel> GetSCJH_HTMXB(Func<XSHTMXBModel, bool> exp, string orderExpr, int? pageIndex, int pageSize, out int totalCount)
        {
            var result = from mxb in dataContext.XSHTMXB.Include("XSHT")
                         join c in dataContext.vCP on mxb.CPID equals c.ID
                         join k in dataContext.ZL_KH on mxb.XSHT.KHID equals k.ID
                         where !dataContext.SCJHMXB.Select(t => t.HTMXBID).Contains(mxb.ID)
                         select new XSHTMXBModel
                         {
                             ID = mxb.ID,
                             XSHTID = mxb.XSHTID,
                             HTZT = mxb.XSHT.HTZT,
                             HTBH = mxb.XSHT.BH,
                             KHJC = k.JIANCHENG,
                             DHSJ = mxb.XSHT.DHSJ,
                             CHSJ = mxb.XSHT.CHSJ,
                             CPID = mxb.CPID,
                             CPBH = c.BH,
                             CPMC = c.CPMC,
                             CPGG = c.CPGG,
                             CPYS = c.YS,
                             CPLX = c.CPLXMC,
                             SL = mxb.SL,
                             DJ = mxb.DJ
                         };
            result = result.Where(exp).AsQueryable();
            totalCount = result.Count();
            if (!string.IsNullOrEmpty(orderExpr))
                result = SortingExtensions.OrderUsingSortExpression(result, orderExpr);
            if (pageIndex == null)
                return result.ToList();
            else
                return result.Skip(Convert.ToInt32(--pageIndex) * pageSize).Take(pageSize).ToList();
        }
    }
}
